package cn.xiaopengstack.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.reactive.function.client.ClientRequest;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.ExchangeFunction;
import reactor.core.publisher.Mono;

/**
 * @author jiangyangang
 */
@Slf4j
public class AiModelCommunicationFilterFunction implements ExchangeFilterFunction {

    @Override
    public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
        // 打印发送给大模型的消息
        log.info("=== 发送给大模型的请求 ===");
        log.info("URL:{}", request.url());
        log.info("Headers:{}", request.headers());


        // 执行请求并打印响应
        return next.exchange(request).doOnNext(response -> {
                    log.info("=== 大模型的响应 ===");
                    log.info("Status:{}", response.statusCode());
                    log.info("Headers:{}", response.headers());
                    // 读取响应体
                    response.bodyToMono(String.class).subscribe(body ->
                            System.out.println("Body: " + body)
                    );
                });
    }
}
